package com.cssl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.Published;
import com.cssl.service.PublishedService;
import com.cssl.vo.PublishedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PublishedController {

    @Autowired
    private PublishedService pservice;

    @PostMapping("/pagepublished")
    public PublishedVo pagepublished(Integer current, Integer size){
        if (current==null||current<1){
            current=1;
        }
        if(size==null||size<1){
            size=5;
        }
        PublishedVo publishedVo = new PublishedVo();
        IPage<Published> page=new Page<>(current,size);
        pservice.page(page,null);
        publishedVo.setCurrent(current);
        publishedVo.setSize(size);
        publishedVo.setTotal(page.getTotal());
        publishedVo.setUserList(page.getRecords());
        publishedVo.setCount(page.getPages());
        return publishedVo;
    }

    //添加
    @PostMapping("/addchu")
    public boolean addchu(Published pl){
        return pservice.save(pl);
    }

    //通过id查询到对象
    @PostMapping("/selpid")
    public Published selid(Integer pid){
        return pservice.getBaseMapper().selectById(pid);
    }


    //通过id去修改
    @PostMapping("/updpid")
    public boolean updbid(Published pl){
        return pservice.updateById(pl);
    }

    //通过id去删除
    @PostMapping("/delpid")
    public int delpid(Integer pid){
        return pservice.getBaseMapper().deleteById(pid);
    }


    @PostMapping("/selpname")
    public Published selpname(int pid){
        return  pservice.selpname(pid);
    }

    @PostMapping("/publishedall")
    public List<Published> publishedall(){
        return pservice.list();
    }
}
